import {createRouter, createWebHashHistory} from 'vue-router'
import login from '@/view/login/index.vue'
import {useUserStore} from '@/pinia/modules/user'


const Router = [
    {path: '/', redirect: '/login'},

    {path: '/login', name: 'Login', component: login},

    {path: '/control', name: '管理员控制台', component: () => import('@/view/control/layout/home.vue'),
        children: [
            {path: 'user', name: "用户管理", component: () => import('@/view/dashboard/user/user.vue')},
            {path: 'authority', name: "权限管理", component: () => import('@/view/dashboard/user/authority.vue')},
            //项目管理
            {path: 'project', name: "项目管理", component: () => import('@/view/dashboard/project/project.vue')},

        ]
    },


    {path: '/dashboard', name: '控制面板', component: () => import('@/view/layout/home.vue'),
        children: [
            {path: 'home', name: "我的看板", component: () => import('@/view/dashboard/board/board.vue')},

            //用户

            {path: 'userinfo', name: "个人信息", component: () => import('@/view/dashboard/user/userinfo.vue'), props: {default: true}},


            {path: 'laboratory', component: () => import('@/view/dashboard/index.vue'),
                children: [
                    {path: 'node', name: "节点管理", component: () => import('@/view/dashboard/laboratory/node.vue')},
                    {path: 'node_server', name: "节点服务", component: () => import('@/view/dashboard/laboratory/node_server.vue')},


                    {path: 'env', name: "环境管理", component: () => import('@/view/dashboard/laboratory/env.vue')},
                ]
            },


            //文章
            {path: 'article', name: "文章", component: () => import('@/view/dashboard/article/article.vue')},
            {path: 'article/list', name: "文章列表", component: () => import('@/view/dashboard/article/articlelist.vue')},
            {path: 'article/sheet', name: "sheet", component: () => import('@/view/dashboard/article/luckysheet.vue')},

            {path: 'tool', name: "工具面板", component: () => import('@/view/dashboard/index.vue'),
                children:[
                    {path: 'home', name: "工具箱", component: () => import('@/view/dashboard/tools/tool.vue')},
                    {path: 'pytool', name: "PYTHON工具", component: () => import('@/view/dashboard/tools/pytool.vue')},
                    {path: 'welibk', name: "welibk", component: () => import('@/view/dashboard/tools/ws.vue')},
                    {path: 'exploratory', name: "探索性测试", component: () => import('@/view/dashboard/tools/exploratory.vue')},
                    {path: 'terminal', name: "终端命令行", component: () => import('@/view/dashboard/tools/terminal.vue')},
                    {path: 'luckysheet', name: "在线表格", component: () => import('@/view/dashboard/tools/luckysheet.vue')},
                    {path: 'json_tool', name: "JSON编辑器", component: () => import('@/view/dashboard/tools/json_tool.vue')},
                    {path: 'fio', name: "FIO", component: () => import('@/view/dashboard/performance/fio.vue')},
                    {path: 'py_socket', name: "PythonWebSocket", component: () => import('@/view/dashboard/tools/websocket_python.vue')},
                    {path: 'Flow', name: "Flow", component: () => import('@/view/dashboard/tools/flow/flow.vue')},
                    {path: 'JupyterLab', name: "JupyterLab", component: () => import('@/view/dashboard/tools/JupyterLab.vue')},
                ],
            },


            {path: 'resultful', name: "接口测试", component: () => import('@/view/dashboard/resultful/resultful.vue')},
            {path: 'performance', name: "性能测试", component: () => import('@/view/dashboard/performance/performance.vue'),
                children:[

                ],
            },

            {path: 'dirtree', name: "目录树", component: () => import('@/view/dashboard/model/dir_tree.vue')},

            // cicd
            {path: 'testcase', name: "用例管理", component: () => import('@/view/dashboard/cicd/testcase.vue')},
            {path: 'task', name: "任务管理", component: () => import('@/view/dashboard/cicd/task.vue')},
            {path: 'histask', name: "历史任务", component: () => import('@/view/dashboard/cicd/histask.vue')},
            {path: 'edittask', name: "任务创建(修改)", component: () => import('@/view/dashboard/cicd/components/task_create.vue')},
            {path: 'pipe', name: "流水线", component: () => import('@/view/dashboard/cicd/pipeline.vue')},
            {path: 'pipe_detail', name: "流水线详情", component: () => import('@/view/dashboard/cicd/pipe_detail.vue')},
            {path: 'hispipe', name: "历史流水线", component: () => import('@/view/dashboard/cicd/his_pipeline.vue')},
            {path: 'editpipe', name: "流水线创建(修改)", component: () => import('@/view/dashboard/cicd/components/PipeEdit.vue')},

            {path: 'resultful', name: "接口测试", component: () => import('@/view/dashboard/resultful/resultful.vue')},

            {path: 'log', name: "审计日志", component: () => import('@/view/dashboard/log/log.vue')},

            {path: 'task_system', name: "自定义任务系统", component: () => import('@/view/dashboard/task_system/task_system.vue')},
        ]
},

]


const router = createRouter({
    history: createWebHashHistory(),
    routes: Router
})

// 设置路由守卫，在进页面之前，判断有token，才进入页面，否则返回登录页面
router.beforeEach((to, from, next) => {
    if (to.matched.some(r => r.meta.requireAuth) !== false) {
        let tokenInfo = useUserStore().token
        if (tokenInfo) {
            next();
        } else {
            next({
                path: "/login", query: {
                    redirect: to.fullPath
                }
            });
        }
    } else {
        useUserStore().Title = to.name
        const store = useUserStore()
        store.RouterList(to)
        next();
    }
});

export default router
